package com.xuanyuanxinyu.compose.ui.common

import android.content.res.Configuration
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import com.xuanyuanxinyu.compose.theme.CustomerTheme

/**
 * @FileName:UI 组件文档
 * @Description:
 * @author ken.luo
 * @date 2023/4/4
 * @version V1.0
 * @email 424533553@qq.com
 */
/**
 * 使用      var showDialog by remember { mutableStateOf(false) } 控制dailog的显示与隐藏
 */
@Composable
fun CommonDialog(
    title : String,
    submitButtonText : String? = "Submit",
    cancelButtonText : String? = "Cancel",
    dialogClick : (Int) -> Unit,
) {
    AlertDialog(onDismissRequest = {
        dialogClick(DIALOG_CLICK_DEFAULT)
    },text = {
        Text(text = title,style = MaterialTheme.typography.bodyMedium)
    },confirmButton = {
        if (!submitButtonText.isNullOrBlank()) {
            TextButton(onClick = {
                dialogClick(DIALOG_CLICK_SUBMIT)
            }) {
                Text(text = submitButtonText)
            }
        }
    },dismissButton = { // 取消按钮
        if (!cancelButtonText.isNullOrBlank()) {
            TextButton(onClick = {
                dialogClick(DIALOG_CLICK_CANCEL)
            }) {
                Text(text = cancelButtonText)
            }
        }
    })
}

const val DIALOG_CLICK_DEFAULT = 0
const val DIALOG_CLICK_CANCEL = -1
const val DIALOG_CLICK_SUBMIT = 1

@Preview(name = "Light Mode")
@Preview(
    uiMode = Configuration.UI_MODE_NIGHT_YES,
    showBackground = true,
    name = "Dark Mode"
)
@Composable
fun CommonDialog() {
    CustomerTheme {
        Surface {
            CommonDialog("标题",dialogClick = { it-> })
        }
    }
}